﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using Hxsoft.AppV1;

namespace Hxsoft.AppV1.Module
{
	public static partial class SystemConfigs
	{
		/// <summary>
		/// 资源代码。
		/// </summary>
		internal const string ResourceCode = "asdf";

		/// <summary>
		/// 资源标识。
		/// </summary>
		internal readonly static Guid ResourceId = new Guid("asdf");

		/// <summary>
		/// 资源名称。
		/// </summary>
		internal const string ResourceName = "asdf";

		/// <summary>
		/// 资源描述。
		/// </summary>
		internal const string ResourceDescription = "";

		/// <summary>
		/// 权限码
		/// </summary>
		public enum PermissionCode
		{
			Insert = 3,
			Update = 7,
			Delete = 15,
		}

		/// <summary>
		/// 注册权限。
		/// </summary>
		internal static void RegisterPermissions()
		{
			ResourceCategories.RegisterSystemManage();
			RegisterCategoryResource();
			RegisterResourcePermissions();
		}

		/// <summary>
		/// 注册资源类别下的资源。
		/// </summary>
		/// <returns>注册的资源。</returns>
		private static Resource RegisterCategoryResource()
		{
			Resource resource = new Resource();
			resource.CategoryId = ResourceCategories.SystemManageResourceCategoryId;
			resource.ResourceId = ResourceId;
			resource.ResourceCode = ResourceCode;
			resource.ResourceName = ResourceName;
			resource.Description = ResourceDescription;

			return Resources.Register(resource);
		}

		/// <summary>
		/// 注册资源下的权限。
		/// </summary>
		private static void RegisterResourcePermissions()
		{
			Permission permission;

			permission = new Permission();
			permission.ResourceId = ResourceId;
			permission.PermissionCode = (int)PermissionCode.Insert;
			permission.PermissionName = "添加";
			permission.Description = "";
			Permissions.Register(permission);

			permission = new Permission();
			permission.ResourceId = ResourceId;
			permission.PermissionCode = (int)PermissionCode.Update;
			permission.PermissionName = "修改";
			permission.Description = "";
			Permissions.Register(permission);

			permission = new Permission();
			permission.ResourceId = ResourceId;
			permission.PermissionCode = (int)PermissionCode.Delete;
			permission.PermissionName = "删除";
			permission.Description = "";
			Permissions.Register(permission);
		}
	}



	/// <summary>
	/// SystemConfigs 类。
	/// </summary>
	public static partial class SystemConfigs
	{
		public const string INFO_TYPE = "SystemConfig";



		#region GetItem
		/// <summary>
		/// 根据标识获取指定的系统配置。
		/// </summary>
		/// <param name="o">系统配置集合。</param>
		/// <param name="siteId">获取的系统配置标识 SiteId。</param>
		/// <param name="name">获取的系统配置标识 Name。</param>
		/// <returns>获取的信息项。</returns>
		public static SystemConfig GetItem(this System.Data.Linq.Table<SystemConfig> o, int siteId, string name)
		{
			return o.SingleOrDefault(p => p.SiteId == siteId && p.Name == name);
		}
		#endregion



		#region Exists
		/// <summary>
		/// 验证指定的系统配置是否已经存在。
		/// </summary>
		/// <param name="o">系统配置集合。</param>
		/// <param name="siteId">验证的系统配置标识 SiteId。</param>
		/// <param name="name">验证的系统配置标识 Name。</param>
		/// <returns>布尔值表示指定的系统配置是否已经存在。</returns>
		public static bool Exists(this System.Data.Linq.Table<SystemConfig> o, int siteId, string name)
		{
			return o.Any(p => p.SiteId == siteId && p.Name == name);
		}
		#endregion
	}



	/// <summary>
	/// SystemConfigs 类操作部分。
	/// </summary>
	public static partial class SystemConfigs
	{
		#region Insert
		/// <summary>
		/// 添加系统配置。
		/// </summary>
		/// <param name="insertedSystemConfig">添加的系统配置。</param>
		/// <returns>添加的系统配置。</returns>
		public static SystemConfig Insert(SystemConfig insertedSystemConfig)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			SystemConfig systemConfig = insertedSystemConfig.ToNewEntity<SystemConfig>();

			GetInsertRight(dc, systemConfig, true);	// 权限验证

			dc.SystemConfigs.InsertOnSubmit(systemConfig);
			dc.SubmitChanges();

			// 记录日志
			Logs.Insert(dc, ResourceId, (int)PermissionCode.Insert, INFO_TYPE, systemConfig.SiteId.ToString(), PermissionCode.Insert.ToString(), systemConfig.Name);

			// 返回只读数据
			return insertedSystemConfig.FillByEntity(systemConfig);
		}
		#endregion



		#region Update
		private static readonly HashSet<string> _updatedExcludeColumns = typeof(SystemConfig).GetEntityColumnNames(new string[] {
				""	// 在这里填写更新的排除列
			}, true);

		/// <summary>
		/// 修改系统配置。
		/// </summary>
		/// <param name="updatedSystemConfig">修改的系统配置。</param>
		/// <returns>修改的系统配置。</returns>
		public static SystemConfig Update(SystemConfig updatedSystemConfig)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			SystemConfig systemConfig = dc.SystemConfigs.GetItem(updatedSystemConfig.SiteId, updatedSystemConfig.Name);
			if (systemConfig == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);

			GetUpdateRight(dc, systemConfig, true);	// 权限验证

			systemConfig.FillByEntity(updatedSystemConfig, EntityColumnsSetType.Exclude, _updatedExcludeColumns);

			try
			{
				dc.SubmitChanges(ConflictMode.ContinueOnConflict);
			}
			catch (ChangeConflictException e)
			{
				foreach (ObjectChangeConflict occ in dc.ChangeConflicts)
				{
					occ.Resolve(RefreshMode.KeepCurrentValues);
				}
			}

			// 记录日志
			Logs.Insert(dc, ResourceId, (int)PermissionCode.Update, INFO_TYPE, systemConfig.SiteId.ToString(), PermissionCode.Update.ToString(), systemConfig.Name);

			// 返回只读数据
			return updatedSystemConfig.FillByEntity(systemConfig);
		}
		#endregion



		#region Delete
		/// <summary>
		/// 删除系统配置。
		/// </summary>
		/// <param name="siteId">删除的系统配置标识 SiteId。</param>
		/// <param name="name">删除的系统配置标识 Name。</param>
		public static void Delete(int siteId, string name)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			SystemConfig systemConfig = dc.SystemConfigs.GetItem(siteId, name);
			if (systemConfig == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);

			GetDeleteRight(dc, systemConfig, true);	// 权限验证

			Delete(dc, systemConfig);

			dc.SubmitChanges();

			// 记录日志
			Logs.Insert(dc, ResourceId, (int)PermissionCode.Delete, INFO_TYPE, systemConfig.SiteId.ToString(), PermissionCode.Delete.ToString(), systemConfig.Name);
		}

		#region internal Delete
		internal static void Delete(GenericModuleDataContext dc, SystemConfig systemConfig)
		{
			dc.SystemConfigs.DeleteOnSubmit(systemConfig);
		}
		#endregion
		#endregion
	}



	/// <summary>
	/// SystemConfigs 类对应操作权限部分。（受限站点资源。）
	/// </summary>
	public static partial class SystemConfigs
	{
		#region GetInsertRight
		/// <summary>
		/// 获取当前用户是否有添加系统配置的权限。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="systemConfig">添加的系统配置对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有添加系统配置的权限。</returns>
		public static bool GetInsertRight(GenericModuleDataContext dc, SystemConfig systemConfig, bool triggerException)
		{
			string userName = Users.CurrentUserName;
			int permissionCode = (int)PermissionCode.Insert;
			bool b = dc.AccessController.GetUserIsHasPermission<SystemConfig>(userName, systemConfig, ResourceId, permissionCode, systemConfig.SiteId);

			if (triggerException && !b)
				throw new PermissionException(userName, systemConfig, ResourceId, permissionCode, Res.PermissionExceptionMessage);
			return b;
		}

		/// <summary>
		/// 获取当前用户是否有添加系统配置的权限。
		/// </summary>
		/// <param name="systemConfig">添加的系统配置对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有添加系统配置的权限。</returns>
		public static bool GetInsertRight(SystemConfig systemConfig, bool triggerException)
		{
			return GetInsertRight(new GenericModuleDataContext(), systemConfig, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有添加系统配置的权限。
		/// </summary>
		/// <param name="systemConfig">添加的系统配置对象。</param>
		/// <returns>布尔值表示当前用户是否有添加系统配置的权限。</returns>
		public static bool GetInsertRight(SystemConfig systemConfig)
		{
			return GetInsertRight(systemConfig, false);
		}
		#endregion



		#region GetUpdateRight
		/// <summary>
		/// 获取当前用户是否有更新系统配置的权限。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="systemConfig">更新的系统配置对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有更新系统配置的权限。</returns>
		public static bool GetUpdateRight(GenericModuleDataContext dc, SystemConfig systemConfig, bool triggerException)
		{
			string userName = Users.CurrentUserName;
			int permissionCode = (int)PermissionCode.Update;
			bool b = dc.AccessController.GetUserIsHasPermission<SystemConfig>(userName, systemConfig, ResourceId, permissionCode, systemConfig.SiteId);

			if (triggerException && !b)
				throw new PermissionException(userName, systemConfig, ResourceId, permissionCode, Res.PermissionExceptionMessage);
			return b;
		}

		/// <summary>
		/// 获取当前用户是否有更新系统配置的权限。
		/// </summary>
		/// <param name="systemConfig">更新的系统配置对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有更新系统配置的权限。</returns>
		public static bool GetUpdateRight(SystemConfig systemConfig, bool triggerException)
		{
			return GetUpdateRight(new GenericModuleDataContext(), systemConfig, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有更新系统配置的权限。
		/// </summary>
		/// <param name="systemConfig">更新的系统配置对象。</param>
		/// <returns>布尔值表示当前用户是否有更新系统配置的权限。</returns>
		public static bool GetUpdateRight(SystemConfig systemConfig)
		{
			return GetUpdateRight(systemConfig, false);
		}
		#endregion



		#region GetDeleteRight
		/// <summary>
		/// 获取当前用户是否有删除系统配置的权限。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="systemConfig">删除的系统配置对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有删除系统配置的权限。</returns>
		public static bool GetDeleteRight(GenericModuleDataContext dc, SystemConfig systemConfig, bool triggerException)
		{
			string userName = Users.CurrentUserName;
			int permissionCode = (int)PermissionCode.Delete;
			bool b = dc.AccessController.GetUserIsHasPermission<SystemConfig>(userName, systemConfig, ResourceId, permissionCode, systemConfig.SiteId);

			if (triggerException && !b)
				throw new PermissionException(userName, systemConfig, ResourceId, permissionCode, Res.PermissionExceptionMessage);
			return b;
		}

		/// <summary>
		/// 获取当前用户是否有删除系统配置的权限。
		/// </summary>
		/// <param name="systemConfig">删除的系统配置对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有删除系统配置的权限。</returns>
		public static bool GetDeleteRight(SystemConfig systemConfig, bool triggerException)
		{
			return GetDeleteRight(new GenericModuleDataContext(), systemConfig, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有删除系统配置的权限。
		/// </summary>
		/// <param name="systemConfig">删除的系统配置对象。</param>
		/// <returns>布尔值表示当前用户是否有删除系统配置的权限。</returns>
		public static bool GetDeleteRight(SystemConfig systemConfig)
		{
			return GetDeleteRight(systemConfig, false);
		}
		#endregion
	}
}